<?php

import("data.db.DbAbstractTable");
import("data.db.DbFactory");

class DbProduct extends DbAbstractTable {
	
	var $className = "data.logic.product.Product";
	const CLASS_NAME =  "data.logic.product.Product";
	
	//
	// SQL Table description section
	//
	const TB_TABLENAME	= "pd_product";
	const TB_I			= 'prod';
	
	const TB_MODEL		= 'prod_model';
	const TB_PUBID		= 'prod_pubid';
	const TB_NAME		= 'prod_name';
	
	function _get_TABLENAME() {return self::TB_TABLENAME;}
	function _get_ID() {return self::TB_I."_id";}
	function _get_SUFFIX() {return self::TB_I;}
	//
	//
	
	function backwardForeignCollection() {
		return array("ProductProperty"=>"data.logic.property.ProductProperty",
					"ImageItem"=>"data.logic.image.ImageItem");
	}
	
	function forwardForeignCollection() {
		return array("Unit"=>"data.logic.product.Unit",
			"Producer"=>"data.logic.producer.Producer");
	}
	
	// properties
	var $model;
	var $name;
	var $pubid;
	
	public function __construct($noinit=false) {
		parent::__construct($noinit);
	}
	
	function getDisplayName() {
		return $this->lingvo->getValue($this->name);
	}
	
	function getName() {
		return $this->name;
	}
	function setName($name) {
		$this->name = $name;
	}
	
	function getModel() {
		return $this->model;
	}
	function setModel($model) {
		$this->model = $model;
	}
	
	function getPubid() {
		return $this->pubid;
	}
	function setPubid($id) {
		$this->pubid = $id;
	}
	
	function setUnit(&$unit) {
		$this->forwardForeigns["Unit"]->setForeign($unit);
	}
	function getUnit() {
		return $this->forwardForeigns["Unit"]->getForeign();
	}
	
	function setProducer(&$producer) {
		$this->forwardForeigns["Producer"]->setForeign($producer);
	}
	function getProducer() {
		return $this->forwardForeigns["Producer"]->getForeign();
	}
	
	function setProductProperties(&$props) {
		$this->backwardForeigns['ProductProperty']->setList($props);
	}
	function getProductProperties() {
		return $this->backwardForeigns['ProductProperty']->getList();
	}
	
	function setImageItems(&$list) {
		$this->backwardForeigns['ImageItem']->setList($list);
	}
	function getImageItems() {
		return $this->backwardForeigns['ImageItem']->getList();
	}
	
	// restore
	function fillEntity($arrResult) {
		parent::fillEntity($arrResult);
		$this->setModel($arrResult[self::TB_MODEL]);
		$this->setPubid($arrResult[self::TB_PUBID]);
		$this->setName($arrResult[self::TB_NAME]);
	}
	
	//store
	function fillFields() {
		parent::fillFields();
		$this->fildsForStore[self::TB_MODEL] = "'".$this->getModel()."'";
		$this->fildsForStore[self::TB_PUBID] = "'".$this->getPubid()."'";
		$this->fildsForStore[self::TB_NAME] = "'".$this->getName()."'";
	}
	
	// static find
	function findByPubid($id) {
		return parent::__selectByCriteria(
			self::TB_PUBID."='".$id."'",
			'data.logic.product.Product');
	}
	
	function findAllByGroupId($group) {
		return parent::__select(
			"prodg_id_prod=prod_id and prodg_id_grpn='".$group."'",
			0, 0, "pd_product_group",
			'data.logic.product.Product');
	}
	
	// same for all classes
	public static function findAll() {return parent::findAll(self::CLASS_NAME);}
    public static function findById($id) {return parent::findById($id, self::CLASS_NAME);}
	
}
?>